package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCacheAnnotation;
import com.baizhi.cmfz.entity.CmfzUser;
import com.baizhi.cmfz.service.CmfzUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class CmfzUserController {
    @Autowired
    private CmfzUserService cus;

    /**
     * 条件查询全部并分页
     */
    @RequestMapping("/selectAllByNameAndTelPage")
    @ResponseBody
    public Map<String, Object> selectAllByNameAndTelPage(String username, String tel,
                                                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        Page<CmfzUser> pi = cus.selectAllByNameAndTelPage(username, tel, page, limit);
        //layui需要的json格式
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", pi.getTotal());
        map.put("data", pi.getRecords());
        return map;
    }


    /**
     * 修改用户信息
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public void updateUser(CmfzUser u) {
        cus.updateUser(u);
    }

    /**
     * 添加用户信息
     */
    @RequestMapping("/insertUser")
    @ResponseBody
    public void insertUser(CmfzUser u) {
        cus.insertUser(u);
    }

    /**
     * 上传图片 此处不再使用虚拟路径 直接上传到项目中去
     */


    /**
     * 根据用户id查询出该用户信息
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public CmfzUser selectById(Integer userId) {
        return cus.selectById(userId);
    }

    /**
     * 查询男女个数
     */
    @RequestMapping("/selectBySexCountByMap")
    @ResponseBody
    public List<Map> selectBySexCountByMap() {
        return cus.selectBySexCountByMap();
    }

    /**
     * 查询各省人数
     */
    @RequestMapping("/selectByProCountByMap")
    @ResponseBody
    public List<Map> selectByProCountByMap() {
        return cus.selectByProCountByMap();
    }


    /**
     * 查询距离现在一周 两周 三周的注册量变化
     */
    @RequestMapping("/selectByDateByMap")
    @ResponseBody
    public List<Map> selectByDateByMap() {
        return cus.selectByDateByMap();
    }

    /**
     * 一个方法里面写三个查询方法 不使用线程
     */
    @RequestMapping("/selectAllCount")
    @ResponseBody
    public Map selectAllCount() {
        long start = System.currentTimeMillis();
        Map map = cus.selectAllCount();
        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");
        return map;
    }

    /**
     * 一个方法里面写三个查询方法 使用线程
     */
    @RequestMapping("/selectAllCountThread")
    @ResponseBody
    public Map selectAllCountThread() throws InterruptedException {
        long start = System.currentTimeMillis();
        Map map = cus.selectAllCountThread();
        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");
        return map;
    }
}